package com.xhtt.modules.reportform.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.inspection.util.InspectDateUtil;
import com.xhtt.modules.reportform.dao.TttReportMapper;
import com.xhtt.modules.reportform.dao.VitReportMapper;
import com.xhtt.modules.reportform.service.TttReportService;
import com.xhtt.modules.reportform.service.VitReportService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 统计分析-VIT
 * </p>
 *
 * @author xj
 * @since 2019-02-14
 */
@Service
public class VitReportServiceImpl extends ServiceImpl<VitReportMapper, Object> implements VitReportService {

    @Autowired
    private TttReportService tttReportService;

    @Override
    public List<Map<String, Object>> total(Map<String, Object> params) {

        List<Map<String, Object>> result = new ArrayList<>();
        Integer type = Integer.parseInt(params.get("type").toString());
        if (type.equals(1)) {
            result = baseMapper.queryTotal(params);
        } else if (type.equals(2)) {
            result = baseMapper.queryTotalOfYear(params);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> discussTime(Map<String, Object> params) {
        return baseMapper.queryDiscussTime(params);
    }

    @Override
    public List<Map<String, Object>> discussProduct(Map<String, Object> params) {
        return baseMapper.queryDiscussProduct(params);
    }

    @Override
    public List<Map<String, Object>> lm(Map<String, Object> params) {
        List<Map<String, Object>> result = new ArrayList<>();
        Integer type = Integer.parseInt(params.get("type").toString());
        if (type.equals(1)) {
            result = baseMapper.queryLm(params);
        } else if (type.equals(2)) {
            result = this.dataOfYear(params, 2);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> berth(Map<String, Object> params) {
        List<Map<String, Object>> result = new ArrayList<>();
        Integer type = Integer.parseInt(params.get("type").toString());
        if (type.equals(1)) {
            result = baseMapper.queryBerth(params);
        } else if (type.equals(2)) {
            result = this.dataOfYear(params, 3);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> dataOfYear(Map<String, Object> params, int type) {

        List<Map<String, Object>> result = new ArrayList<>();
        int year = Integer.parseInt(params.get("year").toString());
        for (int i = 1; i <= 12; i++) {
            Map<String, Object> map = new HashMap<>();
            LocalDateTime l = LocalDateTime.of(year, i, 1, 0, 0, 0);
            List<LocalDateTime> step = InspectDateUtil.getCurrentMonth(l);
            params.put("startDate", step.get(0));
            params.put("endDate", step.get(1));
            List<Map<String, Object>> maps = new ArrayList<>();
            switch (type) {
                case 1: {
                    maps = this.total(params);
                    break;
                }
                case 2: {
                    maps = baseMapper.queryLm(params);
                    break;
                }
                case 3: {
                    maps = baseMapper.queryBerth(params);
                    break;
                }
                case 4: {
                    maps = tttReportService.totalYear(params);
                    break;
                }
            }
            if (CollectionUtils.isNotEmpty(maps)) {
                int sum = maps.stream().mapToInt(item -> Integer.parseInt(item.get("time").toString().substring(0, item.get("time").toString().contains(".") ? item.get("time").toString().indexOf(".") : item.get("time").toString().length()))).sum();
                map.put("sum", sum / 3600 + "时" + sum % 3600 / 60 + "分");
                map.put("time", sum);

                // TTT年份统计
                if (type == 4) {
                    int count = maps.stream().mapToInt(item -> Integer.parseInt(item.get("count").toString())).sum();
                    String time_show = maps.get(0).get("show_time").toString();
                    map.put("count", count);
                    map.put("show_time", time_show);
                }
            }
            map.put("month", InspectDateUtil.replaceNum(i));
            result.add(map);
        }
        return result;
    }

}
